{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from model import DigitClassifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "确认模型总参数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化模型\n",
    "model = DigitClassifier()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 模型层信息 ===\n",
      "Layer: conv1, Module: Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
      "Layer: conv2, Module: Conv2d(32, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
      "Layer: conv3, Module: Conv2d(16, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
      "Layer: fc, Module: Linear(in_features=6272, out_features=10, bias=True)\n"
     ]
    }
   ],
   "source": [
    "# 输出每层信息\n",
    "print(\"=== 模型层信息 ===\")\n",
    "for name, module in model.named_modules():\n",
    "    if name:  # 跳过模型本身（name为空）\n",
    "        print(f\"Layer: {name}, Module: {module}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 每层参数量 ===\n",
      "Parameter: conv1.weight, Shape: torch.Size([32, 1, 3, 3]), Parameters: 288\n",
      "Parameter: conv1.bias, Shape: torch.Size([32]), Parameters: 32\n",
      "Parameter: conv2.weight, Shape: torch.Size([16, 32, 3, 3]), Parameters: 4608\n",
      "Parameter: conv2.bias, Shape: torch.Size([16]), Parameters: 16\n",
      "Parameter: conv3.weight, Shape: torch.Size([8, 16, 3, 3]), Parameters: 1152\n",
      "Parameter: conv3.bias, Shape: torch.Size([8]), Parameters: 8\n",
      "Parameter: fc.weight, Shape: torch.Size([10, 6272]), Parameters: 62720\n",
      "Parameter: fc.bias, Shape: torch.Size([10]), Parameters: 10\n"
     ]
    }
   ],
   "source": [
    "# 输出每层参数量\n",
    "print(\"\\n=== 每层参数量 ===\")\n",
    "for name, param in model.named_parameters():\n",
    "    print(f\"Parameter: {name}, Shape: {param.shape}, Parameters: {param.numel()}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DigitClassifier(\n",
      "  (conv1): Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
      "  (conv2): Conv2d(32, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
      "  (conv3): Conv2d(16, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
      "  (fc): Linear(in_features=6272, out_features=10, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "# 更加简单直接的办法\n",
    "print(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total parameters: 68834\n"
     ]
    }
   ],
   "source": [
    "# 获取总参数量\n",
    "total_params = sum(p.numel() for p in model.parameters())\n",
    "print(f\"Total parameters: {total_params}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
